package com.chuantou.yuba.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.content.Context;

import com.chuantou.yuba.R;

public class DateUtil {

	/** 日期格式 **/
	public static final String PATTERN_DISPMDY = "yy'年'MMMd'日'";

	public static final String PATTERN_DISPMDYHN = "yy'年'MMMd'日' HH'时'mm'分'";

	public static final String PATTERN_MDYHNS = "MM/dd/yyyy HH:mm:ss";

	public static final String PATTERN_YMDHNS = "yyyy-MM-dd HH:mm:ss";

	public static final String PATTERN_YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";

	public static final String PATTERN_HNS = "HH:mm:ss";

	public static final String PATTERN_HN = "HH:mm";

	public static final String PATTERN_MDY = "MM/dd/yyyy";

	public static final String PATTERN_MD = "MM-dd";

	public static final String PATTERN_YMD = "yyyy-MM-dd";

	public static final String PATTERN_MDHM = "MM'月'dd'日' HH:mm";

	public static final String PATTERN_YMDHM = "yyyyMMddHHmm";

	public static final String PATTERN_YYYYMMDD = "yyyy.MM.dd";

	public static String getDateString(long time, String pattern) {
		SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		Date date = new Date(time);
		try {
			String theDate = formatter.format(date);
			return theDate;
		} catch (Exception ex) {
			return null;
		}
	}

	public static Date addDate(Date dt1, Date dt2) {
		long dt1ms = dt1.getTime();
		long dt2ms = dt2.getTime();
		return new Date(dt1ms + dt2ms);
	}

	public static Date addDay(Date dt, int days) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		cal.add(5, days);
		return cal.getTime();
	}

	public static Date addDuration(Date dt, int yr, int mon) {
		Date result = dt;
		result = addMonth(result, mon);
		result = addYear(result, yr);
		return result;
	}

	public static Date addDuration(Date dt, int yr, int mon, int dy) {
		dt = addDay(dt, dy);
		dt = addMonth(dt, mon);
		dt = addYear(dt, yr);
		return dt;
	}

	public static Date addDuration(Date dt, int yr, int mon, int dy, int hr) {
		dt = addHour(dt, hr);
		dt = addDay(dt, dy);
		dt = addMonth(dt, mon);
		dt = addYear(dt, yr);
		return dt;
	}

	public static Date addDuration(Date dt, int yr, int mon, int dy, int hr, int min) {
		dt = addMinute(dt, min);
		dt = addHour(dt, hr);
		dt = addDay(dt, dy);
		dt = addMonth(dt, mon);
		dt = addYear(dt, yr);
		return dt;
	}

	public static Date addDuration(Date dt, int yr, int mon, int dy, int hr, int min, int sec) {
		dt = addSecond(dt, sec);
		dt = addMinute(dt, min);
		dt = addHour(dt, hr);
		dt = addDay(dt, dy);
		dt = addMonth(dt, mon);
		dt = addYear(dt, yr);
		return dt;
	}

	public static Date addHour(Date dt, long hours) {
		long dtms = dt.getTime();
		long hrms = hours * 60L * 60L * 1000L;
		long newdtms = dtms + hrms;
		return new Date(newdtms);
	}

	public static Date addMinute(Date dt, long minutes) {
		long dtms = dt.getTime();
		long minms = minutes * 60L * 1000L;
		long newdtms = dtms + minms;
		return new Date(newdtms);
	}

	public static Date addMonth(Date dt, int months) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int month = cal.get(2);
		month += months;
		int year = month / 12;
		month %= 12;
		cal.set(2, month);
		if (year != 0) {
			int oldYear = cal.get(1);
			cal.set(1, year + oldYear);
		}
		return cal.getTime();
	}

	public static Date addSecond(Date dt, long seconds) {
		long dtms = dt.getTime();
		long secms = seconds * 1000L;
		long newdtms = dtms + secms;
		return new Date(newdtms);
	}

	public static Date addYear(Date dt, int years) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int oldyear = cal.get(1);
		cal.set(1, years + oldyear);
		return cal.getTime();
	}

	public static long diffDate(int interval, Date dt1, Date dt2) {
		Calendar cal = Calendar.getInstance();
		int offset = cal.getTimeZone().getRawOffset();
		long time1 = dt1.getTime() + offset;
		long time2 = dt2.getTime() + offset;
		switch (interval) {
		case 0:
			return getYear(dt1) - getYear(dt2);
		case 1:
			return (getYear(dt1) - getYear(dt2)) * 12 + (getMonth(dt1) - getMonth(dt2));
		case 2:
			int weekDay1 = getWeekDay(dt1);
			int weekDay2 = getWeekDay(dt2);
			Date week1Start = addDay(dt1, -1 * weekDay1);
			Date week2Start = addDay(dt2, -1 * weekDay2);
			return diffDate(3, week1Start, week2Start) / 7L;
		case 3:
			return time1 / 86400000L - time2 / 86400000L;
		case 4:
			return time1 / 3600000L - time2 / 3600000L;
		case 5:
			return time1 / 60000L - time2 / 60000L;
		case 6:
			return time1 / 1000L - time2 / 1000L;
		case 7:

			return dt1.getTime() - dt2.getTime();
		}
		throw new IllegalArgumentException("diffDate(): " + "unknown interval format.");
	}

	public static String getString(Date dt, String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		if (dt == null) {
			return "--";
		}
		return formatter.format(dt);
	}

	public static int getDate(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		return cal.get(5);
	}

	public static int getHour(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		return cal.get(11);
	}

	public static int getMinute(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		return cal.get(12);
	}

	public static int getSecond(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		return cal.get(13);
	}

	public static String getWeekDayName(int dy) throws IllegalArgumentException {
		if ((dy >= 1) && (dy <= 7)) {
			Calendar cal = Calendar.getInstance();
			cal.set(7, dy);
			return getString(cal.getTime(), "EEEE");
		}
		throw new IllegalArgumentException("getDayName(): " + "day value must be between 1 and 7!");
	}

	public static String getWeekDayName(Date dt) {
		return getString(dt, "EEEE");
	}

	public static int getMonth(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		return cal.get(2) + 1;
	}

	public static String getMonthName(int mon) throws IllegalArgumentException {
		mon--;
		if ((mon >= 0) && (mon <= 11)) {
			Calendar cal = Calendar.getInstance();
			cal.set(5, 1);
			cal.set(2, mon);
			return getString(cal.getTime(), "MMMM");
		}
		throw new IllegalArgumentException("getMonthName(): " + "month value must be between 1 and 12!");
	}

	public static String getMonthName(Date dt) {
		return getString(dt, "MMMM");
	}

	public static String getTimeZone(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		TimeZone tz = cal.getTimeZone();
		return tz.getID();
	}

	public static int getWeek(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int week = cal.get(3);
		return week;
	}

	public static int getWeekDay(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		return cal.get(7);
	}

	public static int getYear(Date dt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int yr = cal.get(1);
		int era = cal.get(0);
		if (era == 0) {
			return -1 * yr;
		}
		return yr;
	}

	public static boolean isLeapyear(int year) {
		boolean div4 = year % 4 == 0;
		boolean div100 = year % 100 == 0;
		boolean div400 = year % 400 == 0;
		return (div4) && ((!div100) || (div400));
	}

	public static Date getDateObj(String s, String pattern) {
		SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		try {
			Date theDate = formatter.parse(s);
			return theDate;
		} catch (Exception ex) {
			return null;
		}
	}

	public static boolean isSameDay(Date one, Date other) {
		String oneString = getString(one, PATTERN_YMD);
		String otherString = getString(other, PATTERN_YMD);
		return oneString.equals(otherString);

	}

	public static String getWeekString(Context context) {
		final Calendar calendar = Calendar.getInstance();
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		String str = null;
		switch (dayOfWeek) {
		case Calendar.SUNDAY:
			str = context.getString(R.string.sunday);
			break;
		case Calendar.MONDAY:
			str = context.getString(R.string.monday);
			break;
		case Calendar.TUESDAY:
			str = context.getString(R.string.tuesday);
			break;
		case Calendar.WEDNESDAY:
			str = context.getString(R.string.wednesday);
			break;
		case Calendar.THURSDAY:
			str = context.getString(R.string.thursday);
			break;
		case Calendar.FRIDAY:
			str = context.getString(R.string.friday);
			break;
		case Calendar.SATURDAY:
			str = context.getString(R.string.saturday);
			break;
		default:
			break;
		}
		return str;
	}

}
